package top.shiyiri.preparedstatement;

import org.junit.Test;
import top.shiyiri.bean.Order;
import top.shiyiri.util.JDBCUtils;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @author Aunean
 * @date 2022/1/17 16:09
 */
public class OderForQuery {

    @Test
    public void test1() {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select order_id, order_name, order_date from `order` where order_id = ?";
            ps = connection.prepareStatement(sql);
            ps.setObject(1, 1);

            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                Date date = resultSet.getDate(3);

                Order order = new Order(id, name, date);
                System.out.println(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, ps, resultSet);
        }
    }

    /**
     * @Description 通用的针对 order 表的查询操作
     */
    public static Order orderForQuery(String sql, Object ... args) {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //执行，获取结果集
            rs = ps.executeQuery();
            //获取结果集的元数据
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();//获取列数
            if (rs.next()) {
                Order order = new Order();
                for (int i = 0; i < columnCount; i++) {
                    //获取每个列的列名
                    //String columnName = metaData.getColumnName(i+1);
                    //获取列的别名
                    String columnLabel = metaData.getColumnLabel(i+1);
                    //获取每个列的列值
                    Object columnValue = rs.getObject(i + 1);

                    //通过反射，将对象指定名columnName的属性赋值为指定的值columnValue
                    Field field = Order.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);//保证私有属性也可以访问
                    field.set(order, columnValue);//赋值
                }
                return order;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }
        return null;
    }

    /*
    针对表的字段名与类的属性名不相同的情况：
    1.必须声明sql时，使用类的属性名来命名字段的别名
    2.使用ResultSetMetaData时，需要使用getColumnLabel()来替换getColumnName()，获取列的别名
    说明：如果sql中没有给字段起别名，getColumnLabel()获取的就是列名
     */
    @Test
    public void orderForQueryTest() {
        String sql = "select order_id orderId, order_name orderName, order_date orderDate from `order` where order_id = ?";
        Order order = orderForQuery(sql, 2);
        System.out.println(order);
    }
}
